package com.example.projectmgmt.service.impl;

import com.example.projectmgmt.dto.ProjectDTO;
import com.example.projectmgmt.dto.ProjectDetailDTO;
import com.example.projectmgmt.exception.ResourceConflictException;
import com.example.projectmgmt.exception.ResourceNotFoundException;
import com.example.projectmgmt.model.Project;
import com.example.projectmgmt.repository.ProjectRepository;
import com.example.projectmgmt.service.ProjectService;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ProjectServiceImpl implements ProjectService {

    private final ProjectRepository projectRepository;
    private final ModelMapper modelMapper;

    @Autowired
    public ProjectServiceImpl(ProjectRepository projectRepository, ModelMapper modelMapper) {
        this.projectRepository = projectRepository;
        this.modelMapper = modelMapper;
    }

    @Override
    public List<ProjectDTO> findAllProjects(String search) {
        List<Project> projects;
        if (search != null && !search.isEmpty()) {
            projects = projectRepository.findByProjectNoContainingOrProjectDescContaining(search, search);
        } else {
            projects = projectRepository.findAll();
        }
        return projects.stream()
                .map(project -> modelMapper.map(project, ProjectDTO.class))
                .collect(Collectors.toList());
    }

    @Override
    @Transactional
    public ProjectDTO createProject(ProjectDTO projectDTO) {
        // 检查项目编号是否已存在
        if (projectRepository.existsById(projectDTO.getProjectNo())) {
            throw new ResourceConflictException("项目编号已存在: " + projectDTO.getProjectNo());
        }

        Project project = modelMapper.map(projectDTO, Project.class);
        Project savedProject = projectRepository.save(project);
        return modelMapper.map(savedProject, ProjectDTO.class);
    }

    @Override
    public ProjectDetailDTO findProjectByNo(String projectNo) {
        Project project = projectRepository.findById(projectNo)
                .orElseThrow(() -> new ResourceNotFoundException("项目不存在: " + projectNo));
        ProjectDetailDTO detailDTO = modelMapper.map(project, ProjectDetailDTO.class);
        // 实际实现中会加载阶段和里程碑数据
        return detailDTO;
    }

    @Override
    @Transactional
    public ProjectDTO updateProject(String projectNo, ProjectDTO projectDTO) {
        Project existingProject = projectRepository.findById(projectNo)
                .orElseThrow(() -> new ResourceNotFoundException("项目不存在: " + projectNo));

        // 更新可修改字段
        existingProject.setProjectDesc(projectDTO.getProjectDesc());

        Project updatedProject = projectRepository.save(existingProject);
        return modelMapper.map(updatedProject, ProjectDTO.class);
    }

    @Override
    @Transactional
    public void deleteProject(String projectNo) {
        if (!projectRepository.existsById(projectNo)) {
            throw new ResourceNotFoundException("项目不存在: " + projectNo);
        }
        projectRepository.deleteById(projectNo);
    }
}